const { success } = require("toastr");

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/source/index',
                    add_url: 'business/source/add',
                    edit_url: 'business/source/edit',
                    del_url: 'business/source/del',
                    multi_url: 'business/source/multi',
                    table: 'business_source',
                }
            });

            // 获取到table标签的id
            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                // sortName: 'createtime',
                // sortOrder: 'desc',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'name', title: __('Name') },
                        // {field:'nickname',title:__('Nickname')},
                        // {field:'gender',title:__('Gender')},
                        // {field:'sourceid',title:__('Sourceid')},
                        // {field:'category.name',title:__('CateName')},
                        // {field:'createtime',title:__('Ceatetime'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    recyclebin_url: 'business/source/recyclebin',
                    restore_url: 'business/source/restore',
                    del_url: 'business/source/destroy',
                    table: 'business_source',
                }
            })

            // 获取到table标签的id
            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'createtime',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'name', title: __('Name') },
                        // {field:'nickname',title:__('Nickname')},
                        // {field:'gender',title:__('Gender')},
                        // {field:'sourceid',title:__('Sourceid')},
                        // {field:'category.name',title:__('CateName')},
                        // {field:'createtime',title:__('Ceatetime'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // { field: 'deletetime', title: __('Deletetime'), addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: '还原数据',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-reply',
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    confirm: '确认还原该条数据？',
                                    success: function () {
                                        // 操作完成后是否刷新列表
                                        table.bootstrapTable('refresh')
                                    }
                                }
                            ]
                        }
                    ]
                ]
            })

            $(document).on('click', '.btn-restore', function () {
                Layer.confirm(__('确认还原选择数据？'), function (index) {
                    var ids = Table.api.selectedids(table)

                    // fastAdmin封装ajax请求
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                        data: {
                            ids
                        }
                    }, function () {
                        // 关闭确认框
                        Layer.close(index)

                        // 刷新表格
                        table.bootstrapTable('refresh')
                    })
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }


    return Controller
})